package com.atguigu.wuliupublisher.controller;


import com.atguigu.wuliupublisher.bean.TradeOrgOrderDayBean;
import com.atguigu.wuliupublisher.bean.TrandsTruckStatisticsBean;
import com.atguigu.wuliupublisher.service.TrandsStatsService;
import com.atguigu.wuliupublisher.util.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class TrandsStatsController {

    @Autowired
    private TrandsStatsService trandsStatsService;

    @RequestMapping("/truck")
    public String getTruckStatistics(@RequestParam(value = "date" ,defaultValue = "0") Integer date){
        if (date == 0){
            date = DateFormatUtil.now();
        }
        List<TrandsTruckStatisticsBean> truckStatistics = trandsStatsService.getTruckStatistics(date);
        StringBuilder builder = new StringBuilder("{\"status\": 0,\"data\": {\"columns\": [{\n" +
                "        \"name\": \"卡车类型ID\",\n" +
                "        \"id\": \"truckModelId\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"卡车类型名称\",\n" +
                "        \"id\": \"truckModelName\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"运输里程数(Km)\",\n" +
                "        \"id\": \"mileage\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"运输次数\",\n" +
                "        \"id\": \"ct\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"运输时长(天)\",\n" +
                "        \"id\": \"td\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"平均运输时长(天)\",\n" +
                "        \"id\": \"tdByCt\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"平均运输里程数(Km)\",\n" +
                "        \"id\": \"mileageByCt\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": [");
        int index=0;
        for (TrandsTruckStatisticsBean truckStatistic : truckStatistics) {
            builder.append("{\n" +
                    "        \"truckModelId\": \""+truckStatistic.getTruckModelId()+"\",\n" +
                    "        \"truckModelName\": \""+truckStatistic.getTruckModelName()+"\",\n" +
                    "        \"mileage\": \""+truckStatistic.getMileage()+"\",\n" +
                    "        \"ct\": \""+truckStatistic.getCt()+"\",\n" +
                    "        \"td\": "+truckStatistic.getTd()+",\n" +
                    "        \"tdByCt\": \""+truckStatistic.getTdByCt()+"\",\n" +
                    "       \"mileageByCt\": \""+truckStatistic.getMileageByCt()+"\"\n" +
                    "      }");
            if (++index < truckStatistics.size()){
                builder.append(",");
            }
        }
        builder.append("] }}");
        return builder.toString();
    }

    @RequestMapping("/org")
    public String getOrgOrderDayBase(@RequestParam(value = "date" ,defaultValue = "0") Integer date,
                                     @RequestParam(value = "limit" ,defaultValue = "5") Integer limit){
        if (date == 0){
            date = DateFormatUtil.now();
        }
        List<TradeOrgOrderDayBean> orgOrderDayBase = trandsStatsService.getOrgOrderDayBase(date, limit);
        StringBuilder builder = new StringBuilder("{\"status\": 0, \"data\": [");
        int index=0;
        for (TradeOrgOrderDayBean tradeOrgOrderDayBean : orgOrderDayBase) {
            builder.append("{\n" +
                    "      \"dimValue\": \""+tradeOrgOrderDayBean.getOrgName()+"\",\n" +
                    "      \"dimSubValue\": \""+tradeOrgOrderDayBean.getOrderAmountBase()+"\",\n" +
                    "      \"indicators\": [\n" +
                    "        {\n" +
                    "          \"name\": \"次数\",\n" +
                    "          \"value\": \""+tradeOrgOrderDayBean.getOrderCountBase()+"\",\n" +
                    "          \"hideAlias\": true\n" +
                    "        }\n" +
                    "      ]\n" +
                    "    }");
            if (++index < orgOrderDayBase.size()){
                builder.append(",");
            }
        }
        builder.append("]}");
        return builder.toString();
    }

    @RequestMapping("/amount")
    public String getSumAmount(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if (date == 0){
            date = DateFormatUtil.now();
        }
        Double sumAmount = trandsStatsService.getSumAmount(date);
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"data\": "+sumAmount+"\n" +
                "}";
    }

}
